#include "sqstack.h"
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>



//创建一个长度为len的栈
sqstack * stack_create(int len){
	sqstack * s = (sqstack *)malloc(sizeof(sqstack));
	if(s == NULL){
		printf("init stack failed\n");
		return NULL;
	}
	s->data =(data_t *)malloc(len * sizeof(data_t));
	if(s->data == NULL){
		printf("init stack failed2\n");
		free(s);
		return NULL;		
	}
	s->maxlen = len;
	s->top = -1;
	memset(s->data, 0, len * sizeof(data_t));
	return s;
}
//往栈中放入一个元素
int stack_push(sqstack *s, data_t value){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	if(stack_full(s)){
		printf("s is full\n");
	}
	s->top++;
	s->data[s->top] = value;
	return 0;

}
//从栈中拿出一个元素
data_t stack_pop(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	data_t data = s->data[s->top];
	s->top--;
	return data;
}
//获取栈顶元素
data_t stack_top(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	return s->data[s->top];
	
}
//释放栈空间
int stack_free(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
		return -1;
	}
	free(s->data);
	free(s);
	return 0;
}
//判断栈是否已满
bool stack_full(sqstack *s){
	return s->top == s->maxlen - 1;
}
//判断栈是否已空
bool stack_empty(sqstack *s){
	return s->top == -1;
}

//打印栈列表
void stack_show(sqstack *s){
	if(s == NULL){
		printf("s is NULL\n");
	}
	data_t * data = s->data;
	for(int i = 0 ; i <= s->top ; i++){
		printf("%d ", data[i]);
	}
	puts("");
}
